package com.easyidea.its.web.action.surveillance;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import com.easyidea.its.domain.model.surveillance.CancleSurveillance;
import com.easyidea.its.domain.model.surveillance.Surveillance;
import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.OperationRecord;
import com.easyidea.its.domain.model.system.User;
import com.easyidea.its.domain.service.department.DepartmentService;
import com.easyidea.its.domain.service.operationrecord.OperationRecordService;
import com.easyidea.its.domain.service.surveillance.CancleSurveillanceService;
import com.easyidea.its.domain.service.surveillance.SurveillanceService;
import com.easyidea.its.domain.service.user.UserService;
import com.easyidea.its.util.DateUtil;
import com.easyidea.its.util.PageUtil;
import com.easyidea.its.util.Result;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.action.base.ItsBaseAction;
import com.easyidea.its.web.action.surveillance.dto.SurveillanceDto;

/**
 * 布控
 * 
 * @author kangning.hu
 * 
 */
public class SurveillanceAction extends ItsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -672454624494300677L;
	private SurveillanceService surveillanceService;
	private Surveillance surveillance;
	private DepartmentService departmentService;
	private Department department;
	private UserService userService;
	private SurveillanceDto searchDto;
	private Integer surveillanceID;
	private OperationRecordService operationRecordService;  
	private Result<Surveillance> surveillanceResult;

	private CancleSurveillance cancleSurveillance;
	private CancleSurveillanceService cancleSurveillanceService;
	private Result<CancleSurveillance> cancleSurveillanceResult;

	/**
	 * 新增布控
	 * 
	 * @return
	 */
	public String preAdd() {
		surveillance = new Surveillance();
		if (this.getSessionUser().getDeptId() != null) {
		   department = departmentService.getBeanById(this.getSessionUser().getDeptId());
			surveillance.setSurveillancDept(department);
		}
		surveillance.setStartingDate(DateUtil.timestampNow());
		surveillance.setExpiringDate(new Timestamp(surveillance.getStartingDate().getTime() + 24 * 60 * 60 * 1000));
		return SUCCESS;
	}

	/**
	 * 浏览
	 * 
	 * @return
	 */
	public String preView() {
		// allDepartments = departmentService.getAllDepartments();
		if (surveillance.getSurveillancDept() != null)
			department = departmentService.getBeanById(surveillance.getSurveillancDept().getId());
		return SUCCESS;
	}

	/**
	 * 上一步
	 * 
	 * @return
	 */
	public String preEdit() {
		if (this.getSessionUser().getDeptId() != null) {
		    department = departmentService.getBeanById(this.getSessionUser().getDeptId());
			surveillance.setSurveillancDept(department);
		}
		return SUCCESS;
	}

	/**
	 * 保存成功提示页面
	 * 
	 * @return
	 */
	public String saveSurveillance() {
		User user = userService.getBeanById(this.getSessionUser().getUserId());
		surveillance.setSurveillancUser(user);
		
//		if (!StringUtil.isNullOrBlank(surveillance.getPlateNumberSufix())) {
//			if (surveillance.getPlateNumberSufix().indexOf(surveillance.getPlateNumberPrefix()) >= 0)
//				surveillance.setPlateNumber(surveillance.getPlateNumberSufix());
//			else
//				surveillance.setPlateNumber(surveillance.getPlateNumberPrefix() + surveillance.getPlateNumberSufix());
//		}	
		surveillance.setPlateNumberReg(StringUtil.genarateReg(surveillance.getPlateNumber()));
		surveillanceService.saveOrUpdateBean(surveillance);
		buildOperatorRecord(user,"布控操作");
		return SUCCESS;
	}
	public String detailSurveillance() {
		surveillance = this.surveillanceService.getBeanById(this.surveillanceID);
		if (surveillance.getSurveillancDept() != null)
			department = departmentService.getBeanById(surveillance.getSurveillancDept().getId());		
		return SUCCESS;
	}
	/**
	 * 撤控初始化
	 * 
	 */
	public String preCancel() {
		searchDto = new SurveillanceDto();
		searchDto.setSurveillanceType("1");
		searchDto.setPlateType("");

		Date now = DateUtil.now();
		Date next = new Date(now.getTime() + 24 * 60 * 60 * 1000);
		searchDto.setStartDate(DateUtil.dateToShortStr(now));
		searchDto.setStartHour(0);
		searchDto.setStartMinute(0);
		searchDto.setEndDate(DateUtil.dateToShortStr(next));
		searchDto.setEndHour(23);
		searchDto.setEndMinute(59);

		searchCancel();
		return SUCCESS;
	}

	/**
	 * 布控查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String surveillanceSearch() {
		if (searchDto == null) {
			searchDto = new SurveillanceDto();
			searchDto.setSurveillanceType("1");
			searchDto.setPlateType("");
		}
		Date now = DateUtil.now();
		Date next = new Date(now.getTime() + 24 * 60 * 60 * 1000);
		if (searchDto.getStartDate() == null) {
			searchDto.setStartDate(DateUtil.dateToShortStr(now));
			searchDto.setStartHour(0);
			searchDto.setStartMinute(0);
		}
		if (searchDto.getEndDate() == null) {
			searchDto.setEndDate(DateUtil.dateToShortStr(next));
			searchDto.setEndHour(23);
			searchDto.setEndMinute(59);
		}
		int totalRecords = surveillanceService.getSurveillanceCountByDto(searchDto);
		if (totalRecords > 0) {
			int start = (this.currentPage - 1) * this.getPageSize();
			Collection<Surveillance> records = surveillanceService.getSurveillanceByDto(searchDto, start, this.getPageSize());
			surveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, totalRecords, records);
		} else {
			surveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, 0, new ArrayList());
		}
		return SUCCESS;
	}

	/**
	 * 撤控查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String cancelSsurveillanceSearch() {
		if (searchDto == null) {
			searchDto = new SurveillanceDto();
			searchDto.setSurveillanceType("1");
			searchDto.setPlateType("");
		}
		Date now = DateUtil.now();
		Date next = new Date(now.getTime() + 24 * 60 * 60 * 1000);
		if (searchDto.getStartDate() == null) {
			searchDto.setStartDate(DateUtil.dateToShortStr(now));
		}
		if (searchDto.getEndDate() == null) {
			searchDto.setEndDate(DateUtil.dateToShortStr(next));
			searchDto.setEndHour(23);
			searchDto.setEndMinute(59);
		}	
		int totalRecords = cancleSurveillanceService.getSurveillanceCountByDto(searchDto);
		if (totalRecords > 0) {
			int start = (this.currentPage - 1) * this.getPageSize();
			Collection<CancleSurveillance> records = cancleSurveillanceService.getSurveillanceByDto(searchDto, start, this.getPageSize());
			cancleSurveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, totalRecords, records);
		} else {
			cancleSurveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, 0, new ArrayList());
		}
		return SUCCESS;
	}

	/**
	 * 撤控管理－查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String searchCancel() {
		searchDto.setActive(true);
		int totalRecords = surveillanceService.getSurveillanceCountByDto(searchDto);
		if (totalRecords > 0) {
			int start = (this.currentPage - 1) * this.getPageSize();
			Collection<Surveillance> records = surveillanceService.getSurveillanceByDto(searchDto, start, this.getPageSize());
			surveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, totalRecords, records);
		} else {
			surveillanceResult = PageUtil.createResult(this.getPageSize(), currentPage, 0, new ArrayList());
		}
		return SUCCESS;
	}

	/**
	 * 撤控－表单
	 * 
	 * @return
	 */
	public String doCancel() {
		cancleSurveillance = new CancleSurveillance();
		 department = departmentService.getBeanById(this.getSessionUser().getDeptId());
		this.getSession().setAttribute("search_dto", searchDto);
		return SUCCESS;
	}

	/**
	 * 撤控-保存
	 * 
	 * @return
	 */
	public String saveCancel() {
		User user = userService.getBeanById(this.getSessionUser().getUserId());
		cancleSurveillance.setCancleUser(user);
		cancleSurveillance.setCancleDate(DateUtil.now());
		cancleSurveillanceService.saveCancleSurveillance(cancleSurveillance);
		if (this.getSession().getAttribute("search_dto") != null)
			searchDto = (SurveillanceDto) this.getSession().getAttribute("search_dto");
		else
			searchDto = new SurveillanceDto();
		searchCancel();
		buildOperatorRecord(user,"撤控操作");
		return SUCCESS;
	}

	public SurveillanceService getSurveillanceService() {
		return surveillanceService;
	}

	public void setSurveillanceService(SurveillanceService surveillanceService) {
		this.surveillanceService = surveillanceService;
	}

	public Surveillance getSurveillance() {
		return surveillance;
	}

	public void setSurveillance(Surveillance surveillance) {
		this.surveillance = surveillance;
	}


	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public Result<Surveillance> getSurveillanceResult() {
		return surveillanceResult;
	}

	public void setSurveillanceResult(Result<Surveillance> surveillanceResult) {
		this.surveillanceResult = surveillanceResult;
	}

	public SurveillanceDto getSearchDto() {
		return searchDto;
	}

	public void setSearchDto(SurveillanceDto searchDto) {
		this.searchDto = searchDto;
	}

	public CancleSurveillance getCancleSurveillance() {
		return cancleSurveillance;
	}

	public void setCancleSurveillance(CancleSurveillance cancleSurveillance) {
		this.cancleSurveillance = cancleSurveillance;
	}

	public CancleSurveillanceService getCancleSurveillanceService() {
		return cancleSurveillanceService;
	}

	public void setCancleSurveillanceService(CancleSurveillanceService cancleSurveillanceService) {
		this.cancleSurveillanceService = cancleSurveillanceService;
	}

	public Result<CancleSurveillance> getCancleSurveillanceResult() {
		return cancleSurveillanceResult;
	}

	public void setCancleSurveillanceResult(Result<CancleSurveillance> cancleSurveillanceResult) {
		this.cancleSurveillanceResult = cancleSurveillanceResult;
	}
	public Integer getSurveillanceID() {
		return surveillanceID;
	}

	public void setSurveillanceID(Integer surveillanceID) {
		this.surveillanceID = surveillanceID;
	}
	public OperationRecordService getOperationRecordService() {
		return operationRecordService;
	}

	public void setOperationRecordService(
			OperationRecordService operationRecordService) {
		this.operationRecordService = operationRecordService;
	}
	private void buildOperatorRecord(User user,String operationContent) {
		OperationRecord or = new OperationRecord();
		or.setCreateTime(new Date());
		or.setCreator(user.getUserName());
		or.setModifier(user.getUserName());
		or.setModifyTime(new Date());
		or.setOperationContent(operationContent);
		or.setOperationDate(new Date());
		or.setOperationResult(operationContent + "成功!");
		or.setUser(user);
		operationRecordService.save(or);
	}
}
